package com.wc.fortnight_blue_bridge.Q241024.三个公式;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/24 20:07
 * @description https://www.lanqiao.cn/problems/19956/learning/?contest_id=214
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[] a = new int[N], b = new int[N];
    // a, b的前缀和
    static int[] sa = new int[N], sb = new int[N];
    // a b 1的位置
    static int[] aone = new int[N];
    // b 前缀和奇数和偶数的位置
    static int[] odd = new int[N], even = new int[N];
    static int n;

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        for (int i = 1; i <= n; i++) b[i] = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            sa[i] = sa[i - 1] + a[i];
            sb[i] = sb[i - 1] + b[i];
            if (a[i] == 1) aone[++aone[0]] = i;
            if ((sb[i] & 1) == 1) odd[++odd[0]] = i;
            else even[++even[0]] = i;
        }

        int res = 0;
        aone[++aone[0]] = n + 1;
        int len = aone[0];
        aone[0] = 0;

        // 0 0 0
        for (int i = 1; i <= len; i++) {
            int left = aone[i - 1] + 1, right = aone[i] - 1;
            if (left > right) continue;
            // 里面全是0
            for (int j = left; j <= right; j++) {
                // 设置 x = j, 找y
                if ((sb[j - 1] & 1) == 1) {
                    // 找奇数位置满足小于等于 right
                    int l = 1, r = odd[0];
                    while (l < r) {
                        int mid = l + r + 1 >> 1;
                        if (odd[mid] <= right) l = mid;
                        else r = mid - 1;
                    }

                    if (odd[r] >= j) res = Math.max(res, odd[r] - j + 1);
                } else {
                    // 找偶数位置满足小于等于 right
                    int l = 1, r = even[0];
                    while (l < r) {
                        int mid = l + r + 1 >> 1;
                        if (even[mid] <= right) l = mid;
                        else r = mid - 1;
                    }
                    if (even[r] >= j) res = Math.max(res, even[r] - j + 1);
                }
            }
        }

        // 1 1 0
        for (int i = 1; i < len; i++) {
            // 这个表示中间的a 1
            int left = aone[i - 1] + 1, right = aone[i + 1] - 1;
            if (left > right) continue;
            // 遍历左边, 找右边
            for (int j = left; j <= aone[i]; j++) {
                if ((sb[j - 1] & 1) == 1) {
                    // 找奇数位置满足小于等于 right
                    int l = 1, r = odd[0];
                    while (l < r) {
                        int mid = l + r + 1 >> 1;
                        if (odd[mid] <= right) l = mid;
                        else r = mid - 1;
                    }
                    if (odd[r] >= aone[i]) res = Math.max(res, odd[r] - j + 1);
                } else {
                    // 找偶数位置满足小于等于 right
                    int l = 1, r = even[0];
                    while (l < r) {
                        int mid = l + r + 1 >> 1;
                        if (even[mid] <= right) l = mid;
                        else r = mid - 1;
                    }
                    if (even[r] >= aone[i]) res = Math.max(res, even[r] - j + 1);
                }
            }
        }

        // 2 1 1
        for (int i = 1; i < len - 1; i++) {
            // i 是第一个1, i + 1 是第二个 1
            int left = aone[i - 1] + 1, right = aone[i + 2] - 1;
            if (left > right) continue;
            for (int j = left; j <= aone[i]; j++) {
                if ((sb[j - 1] & 1) == 1) {
                    // 找偶数位置满足小于等于 right
                    int l = 1, r = even[0];
                    while (l < r) {
                        int mid = l + r + 1 >> 1;
                        if (even[mid] <= right) l = mid;
                        else r = mid - 1;
                    }
                    if (even[r] >= aone[i + 1]) res = Math.max(res, even[r] - j + 1);
                } else {
                    // 找奇数位置满足小于等于 right
                    int l = 1, r = odd[0];
                    while (l < r) {
                        int mid = l + r + 1 >> 1;
                        if (odd[mid] <= right) l = mid;
                        else r = mid - 1;
                    }
                    if (odd[r] >= aone[i + 1]) res = Math.max(res, odd[r] - j + 1);
                }
            }
        }

        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
